package com.sfac.javaSpringBoot.aop;



/*
* 打印控制器request 信息
* 打印指定方法调用执行时间
* */

import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.omg.CORBA.StringHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class MyAop {
    private final static Logger LOGGER = LoggerFactory.getLogger(MyAop.class);
    /**
     * -关联在方法上的切点，大面积额切入
     * -第一个 * 代表返回类型不限
     * -第二个 * 代表 module 下所有子包
     * -第三个 * 代表所有类
     * -第四个 * 代表所有方法
     * (..) 代表参数不限
     * Order 代表优先级，数字越小优先级越高
     */
    @Pointcut("execution(public * com.sfac.javaSpringBoot.modules.*.controller.*.*(..))")
    @Order(value = 1)
    public void controllerJoinPoint(){}
//    自定义注解精确切点
    @Pointcut("@annotation(com.sfac.javaSpringBoot.aop.MethodCallTimeAnnotation)")
    @Order(value = 1)
    public void methodCallTimeJoinPoint(){ }

    @Before(value = "controllerJoinPoint()")
    public void beforeController(JoinPoint joinPoint){
        LOGGER.debug("=======Before Controller");
    }
    @Around(value = "controllerJoinPoint()")
    public Object aroundController(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.debug("=======around Controller========");
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        LOGGER.debug(String.format("RemoteAddr: %s", request.getRemoteAddr()));
        LOGGER.debug(String.format("RequestURI: %s", request.getRequestURI()));
        LOGGER.debug(String.format("RemoteMethod: %s", request.getMethod()));
        LOGGER.debug(String.format("class: %s", proceedingJoinPoint.getTarget().getClass()));
        LOGGER.debug(String.format("Signature: %s", proceedingJoinPoint.getSignature().getName()));
        LOGGER.debug(String.format("Args: %s", proceedingJoinPoint.getArgs()==null?"":
                proceedingJoinPoint.getArgs()));
        return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }

    @Before(value = "controllerJoinPoint()")
    public void afterController(JoinPoint joinPoint){
        LOGGER.debug("=======after Controller==========");
    }

    //注解切面通知
    @Around(value = "methodCallTimeJoinPoint()")
    public Object aroundAnnotation(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.debug("=======around Annotation======");
        Long start = System.currentTimeMillis();
        Object obj = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        Long end = System.currentTimeMillis();
        LOGGER.debug(String.format("Call method time :%s ms.", (end-start)));
        return obj;
//        return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }
}
